package com.bjbn.util;

import com.bjbn.entity.EqptMapping;
import com.bjbn.service.EqptMappingCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 资产映射缓存工具类
 * 提供便捷的静态方法供业务层使用
 */
@Component
public class EqptMappingCacheUtil {
    
    private static EqptMappingCacheService eqptMappingCacheService;
    
    @Autowired
    public void setEqptMappingCacheService(EqptMappingCacheService eqptMappingCacheService) {
        EqptMappingCacheUtil.eqptMappingCacheService = eqptMappingCacheService;
    }
    
    /**
     * 根据设备编码获取资产映射
     * @param eqptCode 设备编码
     * @return 资产映射对象，未找到返回null
     */
    public static EqptMapping getByEqptCode(String eqptCode) {
        if (eqptMappingCacheService == null) {
            return null;
        }
        return eqptMappingCacheService.getByEqptCode(eqptCode);
    }
    
    /**
     * 根据BMS编码获取资产映射
     * @param bmsCode BMS编码
     * @return 资产映射对象，未找到返回null
     */
    public static EqptMapping getByBmsCode(String bmsCode) {
        if (eqptMappingCacheService == null) {
            return null;
        }
        return eqptMappingCacheService.getByBmsCode(bmsCode);
    }
    
    /**
     * 根据资产编码获取资产映射
     * @param assetCode 资产编码
     * @return 资产映射对象，未找到返回null
     */
    public static EqptMapping getByAssetCode(String assetCode) {
        if (eqptMappingCacheService == null) {
            return null;
        }
        return eqptMappingCacheService.getByAssetCode(assetCode);
    }
    
    /**
     * 根据BA编码获取资产映射
     * @param baCode BA编码
     * @return 资产映射对象，未找到返回null
     */
    public static EqptMapping getByBaCode(String baCode) {
        if (eqptMappingCacheService == null) {
            return null;
        }
        return eqptMappingCacheService.getByBaCode(baCode);
    }
    
    /**
     * 获取所有资产映射数据
     * @return 所有资产映射列表
     */
    public static List<EqptMapping> getAllMappings() {
        if (eqptMappingCacheService == null) {
            return null;
        }
        return eqptMappingCacheService.getAllMappings();
    }
    
    /**
     * 根据资产类型获取资产映射列表
     * @param assetType 资产类型
     * @return 资产映射列表
     */
    public static List<EqptMapping> getByAssetType(String assetType) {
        if (eqptMappingCacheService == null) {
            return null;
        }
        return eqptMappingCacheService.getByAssetType(assetType);
    }
    
    /**
     * 获取缓存统计信息
     * @return 缓存统计信息
     */
    public static Map<String, Object> getCacheStats() {
        if (eqptMappingCacheService == null) {
            return null;
        }
        return eqptMappingCacheService.getCacheStats();
    }
    
    /**
     * 手动刷新缓存
     */
    public static void refreshCache() {
        if (eqptMappingCacheService != null) {
            eqptMappingCacheService.refreshCache();
        }
    }
    
    /**
     * 检查缓存是否已初始化
     * @return true-已初始化，false-未初始化
     */
    public static boolean isCacheInitialized() {
        return eqptMappingCacheService != null && eqptMappingCacheService.getAllMappings() != null;
    }
}
